<template>
    <div class="box-card box-minheight">
        <div class="card-header">
            <span>{{ curRouteName }}</span>
        </div>
        <div class="box-content">
            <el-row>
                <el-col :span="12" :xs="24" :sm="18" :md="18" :lg="8" :xl="8">
                    <el-form ref="form" :model="formData" :rules="formRules" label-width="120px" label-position="right">
                        <el-form-item label="用户名" prop="username">
                            <el-input v-model="formData.username"></el-input>
                        </el-form-item>
                        <el-form-item label="新密码" prop="password">
                            <el-input type="password" v-model="formData.password"></el-input>
                        </el-form-item>
                        <el-form-item label="新密码确认" prop="confirmPassword">
                            <el-input type="password" v-model="formData.confirmPassword"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="submitForm">确定</el-button>
                        </el-form-item>
                    </el-form>
                </el-col>
            </el-row>
        </div>
    </div>
</template>

<script setup lang="ts">
import { useRoute } from 'vue-router'
import { ref, computed } from 'vue'
//获取路由对象
let $route = useRoute()
// 计算出当前路由名称
let curRouteName = computed(() => {
    return $route.matched[$route.matched.length - 1].meta.title
})

const form = ref(null)
const formData = ref({
    username: '',
    password: '',
    confirmPassword: '',
});

const validatePassword = (rule: any, value: string, callback: any) => {
    const pattern = /^[a-zA-Z0-9]{4,12}$/;
    if (!pattern.test(value)) {
        callback(new Error('新密码必须由4-12位数字或字母组成'));
    } else {
        callback();
    }
};

const validateConfirmPassword = (rule: any, value: string, callback: any) => {
    if (value !== formData.value.password) {
        callback(new Error('两次输入的密码不一致'));
    } else {
        callback();
    }
};
const formRules = {
    username: [
        { required: true, message: '请输入用户名', trigger: 'change' },
    ],
    password: [
        { required: true, message: '请输入新密码', trigger: 'change' },
        { validator: validatePassword, trigger: 'change' },
    ],
    confirmPassword: [
        { required: true, message: '请再次输入新密码', trigger: 'change' },
        { validator: validateConfirmPassword, trigger: 'change' },
    ],
};



const submitForm = () => {
    form.value.validate((valid: boolean) => {
        if (valid) {
            // 表单验证通过，可以进行提交操作
            // ...
        } else {
            // 表单验证不通过，进行错误处理
            return false;
        }
    });
};


</script>

<style scoped lang></style>